package com.zhangxu.microservice.system.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.common.exception.BizException;
import com.zhangxu.microservice.common.utils.AssertUtils;
import com.zhangxu.microservice.system.biz.convert.ApplicationConverter;
import com.zhangxu.microservice.system.biz.dto.ApplicationDTO;
import com.zhangxu.microservice.system.biz.dto.CreateApplicationDTO;
import com.zhangxu.microservice.system.biz.dto.UpdateApplicationDTO;
import com.zhangxu.microservice.system.biz.service.ApplicationService;
import com.zhangxu.microservice.system.dao.condition.ApplicationCondition;
import com.zhangxu.microservice.system.dao.entity.ApplicationDO;
import com.zhangxu.microservice.system.dao.enums.ApplicationStatusEnum;
import com.zhangxu.microservice.system.dao.service.ApplicationDao;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 系统管理服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ApplicationServiceImpl implements ApplicationService {

    private final ApplicationDao applicationDao;
    private final ApplicationConverter applicationConverter;

    @Override
    public Long createApplication(CreateApplicationDTO createApplicationDTO) {
        // 检查系统编码是否已存在
        ApplicationDO existingApplication = applicationDao.getByCode(createApplicationDTO.getCode());
        if (existingApplication != null) {
            throw new BizException("系统编码已存在");
        }

        // 转换并保存系统
        ApplicationDO applicationDO = applicationConverter.toApplicationDO(createApplicationDTO);
        applicationDO.setDeleteTime(0L);
        applicationDO.setStatus(ApplicationStatusEnum.NEW);
        applicationDao.save(applicationDO);
        return applicationDO.getId();
    }

    @Override
    public void updateApplication(UpdateApplicationDTO updateApplicationDTO) {
        // 检查系统是否存在
        ApplicationDO existingApplication = applicationDao.getById(updateApplicationDTO.getId());
        AssertUtils.notNull(existingApplication, "系统不存在");
        AssertUtils.isTrue(ApplicationStatusEnum.NEW.equals(existingApplication.getStatus()), "当前状态不能编辑");

        // 如果修改了系统编码，需要检查新编码是否已存在
        if (!existingApplication.getCode().equals(updateApplicationDTO.getCode())) {
            ApplicationDO applicationWithNewCode = applicationDao.getByCode(updateApplicationDTO.getCode());
            if (applicationWithNewCode != null) {
                throw new BizException("系统编码已存在");
            }
        }

        // 转换并更新系统
        ApplicationDO systemDO = applicationConverter.toApplicationDO(updateApplicationDTO);
        applicationDao.updateById(systemDO);
    }

    @Override
    public void deleteApplication(Long systemId) {
        // 检查系统是否存在
        ApplicationDO existingApplication = applicationDao.getById(systemId);
        AssertUtils.notNull(existingApplication, "系统不存在");
        AssertUtils.isTrue(ApplicationStatusEnum.NEW.equals(existingApplication.getStatus()), "当前状态不能删除");

        // 逻辑删除系统
        ApplicationDO applicationDO = new ApplicationDO();
        applicationDO.setId(systemId);
        applicationDO.setDeleteTime(System.currentTimeMillis());
        applicationDao.updateById(applicationDO);
    }

    @Override
    public void enableApplication(Long applicationId) {
        // 检查系统是否存在
        ApplicationDO existingApplication = applicationDao.getById(applicationId);
        AssertUtils.notNull(existingApplication, "系统不存在");
        AssertUtils.isFalse(ApplicationStatusEnum.ENABLE.equals(existingApplication.getStatus()), "当前状态不能启用");

        // 启用系统
        ApplicationDO applicationDO = new ApplicationDO();
        applicationDO.setId(applicationId);
        applicationDO.setStatus(ApplicationStatusEnum.ENABLE);
        applicationDao.updateById(applicationDO);
    }

    @Override
    public void disableApplication(Long applicationId) {
        // 检查系统是否存在
        ApplicationDO existingSystem = applicationDao.getById(applicationId);
        AssertUtils.notNull(existingSystem, "系统不存在");
        AssertUtils.isTrue(ApplicationStatusEnum.ENABLE.equals(existingSystem.getStatus()), "当前状态不能禁用");

        // 禁用系统
        ApplicationDO applicationDO = new ApplicationDO();
        applicationDO.setId(applicationId);
        applicationDO.setStatus(ApplicationStatusEnum.DISABLE);
        applicationDao.updateById(applicationDO);
    }

    @Override
    public ApplicationDTO getApplicationById(Long applicationId) {
        ApplicationDO applicationDO = applicationDao.getById(applicationId);
        return applicationConverter.toApplicationDTO(applicationDO);
    }

    @Override
    public ApplicationDTO getApplicationByCode(String appCode) {
        ApplicationDO applicationDO = applicationDao.getByCode(appCode);
        return applicationConverter.toApplicationDTO(applicationDO);
    }

    @Override
    public List<ApplicationDTO> listApplicationByIds(Collection<Long> applicationIds) {
        List<ApplicationDO> applicationDOS = applicationDao.listByIds(applicationIds);
        return applicationConverter.toApplicationDTOs(applicationDOS);
    }

    @Override
    public IPage<ApplicationDTO> page(ApplicationCondition condition) {
        IPage<ApplicationDO> page = applicationDao.page(condition);
        return page.convert(applicationConverter::toApplicationDTO);
    }
}